﻿
#include <assert.h>
#include "sdk/message/Common.h"
#include "MD5.h"
#include "sdk/log/QLLog.h"
#include "sdk/message/define.h"
#include "Util.h"
#include "base/ccUTF8.h"
#include <string.h>
#include "sdk/network/SocketTCP.h"
#include "sdk/util/wechatutil/WeChatLogin.h"
#include "sdk/util/wechatutil/WeChatLoginAndroid.h"
#include "sdk/ui/UpgradeLayer.h"
#include "sdk/Sound/ArmFun.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "../cocos2d/cocos/platform/android/jni/JniHelper.h"
#include "../cocos2d/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include <netdb.h>
#include <sys/socket.h>
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "Util_ios.h"
#include "sdk/ui/OpenUrlIOS.h"
#include "sdk/util/wechatutil/WeChatLoginIOS.h"
#endif

#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include <regex>
#include <WS2tcpip.h>
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include <regex.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>
#endif

#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
#include "sdk/ui/WebView.h"
#endif

#define TAG "Util"

//工具java类
const char* ANDROID_QLUTIL_PACKAGE		= "org/cocos2dx/cpp/QLUtil";
const char* ANDROID_APPLIST_PACKAGE		= "org/cocos2dx/cpp/AppList";
const char* ANDROID_APPACTIVITY_PACKAGE = "org/cocos2dx/cpp/AppActivity";

using namespace cocos2d;

// 获取字符串宽度
float getUTF8StringWidth(const char* str ,const char* fontName ,float fontSize) {
	Texture2D txt;
	txt.initWithString(str, fontName, fontSize);
	return txt.getContentSize().width;
}

// 获取一个 UTF8 字符的字节数
uint32_t getUTF8CharLength(char ch) {
	if (0 == (ch >> 7 & 1)){
		return 1;
	}

	for (uint32_t i = 7; i > 0; --i) {
		if (0 == (ch >> i & 1)){
			return (7 - i);
		}
	}

	return 0;
}

//高级位置UTF8
uint32_t advancePosUTF8(const char* str ,uint32_t start ,uint32_t numAdvance) {
	const uint32_t len = strlen(str);
	uint32_t pos = start;

	for (uint32_t i = 0; i < numAdvance && pos < len; ++i){
		pos += getUTF8CharLength(str[pos]);
	}

	return (pos - start);
}

const char* numToShortString(long long num) {
	if (num < 0) {
		return "0000";
	}
	String *str = String::createWithFormat("%lld", num);
	const char* chars = str->getCString();

	int len = str->length();
	if (len > 4) {
		char numChars[4] = { 0 };
		for (int i = 0; i < 4; i++) {
			numChars[i] = chars[i];
		}
		int pointPos = len % 4;
		int afterPoint = 0;
		if (pointPos > 0) {
			for (int i = 3; i >= pointPos; i--) {
				if (numChars[i] == '0') {
					numChars[i] = NULL;
				} else {
					afterPoint = i - pointPos + 1;
					break;
				}
			}
		} else {
			pointPos = 4;
		}

		char ret[6] = { 0 };
		int index = 0;
		for (; index < pointPos; index++) {
			ret[index] = numChars[index];
		}
		if (afterPoint > 0) {
			ret[index++] = '.';
			for (int i = 0; i < afterPoint; i++) {
				ret[index] = numChars[index - 1];
				index++;
			}
		}

		chars = str->getCString();
	}
	return chars;
}

const char* numToShortStringWithUnit(long long num) {
	if (num < 0) {
		return "0000";
	}

	String *str = String::createWithFormat("%lld", num);
	const char* chars = str->getCString();

	int len = str->length();
	if (len > 4) {
		char numChars[4] = { 0 };
		for (int i = 0; i < 4; i++) {
			numChars[i] = chars[i];
		}
		int pointPos = len % 4;
		int afterPoint = 0;
		if (pointPos > 0) {
			for (int i = 3; i >= pointPos; i--) {
				if (numChars[i] == '0') {
					numChars[i] = NULL;
				} else {
					afterPoint = i - pointPos + 1;
					break;
				}
			}
		} else {
			pointPos = 4;
		}

		char ret[6] = { 0 };
		int index = 0;
		for (; index < pointPos; index++) {
			ret[index] = numChars[index];
		}
		if (afterPoint > 0) {
			ret[index++] = '.';
			for (int i = 0; i < afterPoint; i++) {
				ret[index] = numChars[index - 1];
				index++;
			}
		}

		if (len > 8) {
			str->initWithFormat("%s亿", ret);
		} else {
			str->initWithFormat("%s万", ret);
		}

		chars = str->getCString();
	}
	return chars;
}

const char* getStrWithWidthLimit(const char* str ,float maxWidth ,const char* fontName ,float fontSize ,string& lastSafe) {
	if (getUTF8StringWidth(str, fontName, fontSize) <= maxWidth) {
		return str;
	}

	string orig(str);
	int pos		= advancePosUTF8(str, 0, 3);        // 先获取三个字符
	int length	= orig.length();

	for (; pos < length;) {
		string tmp(orig.begin(), orig.begin() + pos);
		tmp += "...";
		if (getUTF8StringWidth(tmp.c_str(), fontName, fontSize) <= maxWidth) {
			lastSafe = tmp.c_str();
			pos += advancePosUTF8(str, pos, 1);
		} else {
			lastSafe = tmp.c_str();
			break;
		}
	}

	WH_LogErr(TAG, "getStrWithWidthLimit, str=%s, ret=%s", str, lastSafe.c_str());
	return lastSafe.c_str();
}

BYTE Util::getDeviceType() {
	BYTE deviceType = 0;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	deviceType = DEVICE_TYPE_ANDROID;
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	deviceType = DEVICE_TYPE_IPHONE;
#endif
	return deviceType;
}

bool Util::getDeviceMD5(TCHAR deviceMd5[LEN_MD5]) {
	return true;
}

bool Util::createTCHARWithString(std::string str ,TCHAR* dest) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	char * pTemp = (char *)(str.c_str());
	MultiByteToWideChar(CP_ACP,0,pTemp,-1,dest,strlen(pTemp));
#else
	std::u16string utf16String;
	StringUtils::UTF8ToUTF16(str, utf16String);
	for (int i = 0; i < utf16String.length(); i++){
		dest[i] = utf16String[i];
	}
#endif
	return true;
}

//string -> MD5 NSString
std::string Util::strToMd5Str(std::string str) {
	std::string resStr;
	CMd5 md5;
	const char* cStr = str.c_str();
	unsigned char* result;

	result = (unsigned char*) md5.MakeMD5(reinterpret_cast<const BYTE*>(cStr), str.length());

	std::string desStr = StringUtils::format("%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3],
			result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]);
	return desStr;
}

int Util::gameRoomType(unsigned long serverRule) {
	if ((serverRule & SR_GAMEMATCH_SCORE) != 0){
		return 1;
	}
	if ((serverRule & SR_GAMEMATCH_WASHOUT) != 0){
		return 2;
	}

	return 0;
}

std::string Util::getStringWithTChar(TCHAR * pChar) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	std::string str = "";
	int iLen = WideCharToMultiByte(CP_ACP , 0,pChar,-1,NULL,0,NULL,NULL);
	char * chRtn = new char[iLen*sizeof(char)];
	WideCharToMultiByte(CP_ACP,0,pChar,-1,chRtn,iLen,NULL,NULL);
	str =chRtn;
	str = UnicodeToUTF8(str);
	CC_SAFE_DELETE_ARRAY(chRtn);
	return str;
#else
	std::string strUtf8 = "";
	std::basic_string<unsigned short> strUtf16 = pChar;
	if (!strUtf16.empty()) {
		StringUtils::UTF16ToUTF8((std::u16string&) (strUtf16), strUtf8);
		return strUtf8;
	}
	return strUtf8;
#endif
	return "";
}

bool Util::checkRegex(string str ,string reg) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	regex pattern(reg);
	if(!regex_match(str,pattern))
	{
		return false;
	}
#elif(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	char ss[1024];
	ZeroMemory(ss, sizeof(ss));
	const char* pStr = str.c_str();
	strcpy(ss,pStr);

	regmatch_t pmatch[4];
	regex_t match_regex;
	regcomp(&match_regex , reg.c_str() ,REG_EXTENDED);
	if(regexec(&match_regex , ss, 4, pmatch , 0) != 0)
	{
		return false;
	}
	regfree(&match_regex);
#endif
	return true;
}

std::string Util::UnicodeToUTF8(const std::string str) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	char * pElementText;
	int nwLen;
	nwLen = MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0);
	wchar_t * pwBuf = new wchar_t[nwLen+1];
	memset(pwBuf,0,nwLen*2+2);
	::MultiByteToWideChar(CP_ACP,0,str.c_str(),str.length(),pwBuf,nwLen);
	int nLen = ::WideCharToMultiByte(CP_UTF8,0,pwBuf,-1,NULL,NULL,NULL,NULL);
	char * pBuf = new char[nLen+1];
	memset(pBuf,0,nLen+1);
	::WideCharToMultiByte(CP_UTF8,0,pwBuf,nwLen,pBuf,nLen,NULL,NULL);
	std::string reStr(pBuf);
	delete []pBuf;
	delete []pwBuf;
	pBuf = NULL;
	pwBuf = NULL;
	return reStr;
#else
	return str;
#endif
	return "";
}

//去除前后空格
string& Util::trim(string& str) {
	if (str.empty()) {
		return str;
	}

	str.erase(0, str.find_first_not_of(" "));
	str.erase(str.find_last_not_of(" ") + 1);
	return str;
}

std::string Util::UTF8ToUnicode(const std::string str) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	int nwLen= MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0);
	wchar_t * pwBuf = new wchar_t[nwLen+1];
	memset(pwBuf , 0, nwLen*2+2);
	MultiByteToWideChar(CP_UTF8,0,str.c_str(),str.length(),pwBuf,nwLen);
	int nLen = WideCharToMultiByte(CP_ACP,0,pwBuf,-1,NULL,NULL,NULL,NULL);
	char * pBuf = new char[nLen+1];
	WideCharToMultiByte(CP_ACP,0,pwBuf,nwLen,pBuf,nLen,NULL,NULL);
	std::string reStr = pBuf;
	delete []pBuf;
	delete []pwBuf;
	pBuf	= NULL;
	pwBuf	= NULL;
	return reStr;
#else
	return str;
#endif
	return "";
}

std::string Util::getStringWithTCharShort(StringW &strUtf16) {
	std::string strUtf8 = "";
	if (!strUtf16.empty()) {
		StringUtils::UTF16ToUTF8((std::u16string&) (strUtf16), strUtf8);
		return strUtf8;
	}
	return strUtf8;
}

const char* Util::getIpByHostName(char* strHostName) {
	hostent* host_entry = gethostbyname(strHostName);
	char IPStr[64] = { 0 };
	if (host_entry != 0) {
		sprintf(IPStr, "%d.%d.%d.%d", (host_entry->h_addr_list[0][0] & 0x00ff), (host_entry->h_addr_list[0][1] & 0x00ff),
				(host_entry->h_addr_list[0][2] & 0x00ff), (host_entry->h_addr_list[0][3] & 0x00ff));
		String* str			= String::createWithFormat("%s", IPStr);
		const char* chars	= str->getCString();
		return chars;
	}
	return strHostName;
}

std::string Util::TCHARToChar(TCHAR* src) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	std::basic_string<unsigned short> str = src;
	return Util::getStringWithTCharShort(str);
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	std::basic_string<unsigned short> str = src;
	return Util::getStringWithTCharShort(str);
#else
	std::string str = "";
	char * chText;
	DWORD dwNum = WideCharToMultiByte(CP_UTF8, 0, src, -1, NULL, NULL, NULL, NULL);
	chText = new char[dwNum + 1];
	memset(chText, 0, dwNum + 1);
	WideCharToMultiByte(CP_UTF8, 0, src, -1, chText, dwNum, NULL, NULL);
	str = chText;
	CC_SAFE_DELETE_ARRAY(chText);
	return str;
#endif
	return "";
}

//截取
void Util::split(const std::string& s ,const std::string& delim ,std::vector<std::string>* ret) {
	size_t last = 0;
	size_t index = s.find_first_of(delim, last);
	while (index != std::string::npos) {
		ret->push_back(s.substr(last, index - last));
		last = index + 1;
		index = s.find_first_of(delim, last);
	}
	if (index - last > 0) {
		ret->push_back(s.substr(last, index - last));
	}
}

////////////////////////////////调用jni接口//////////////////////////
void Util::vibrate() {
	WH_LogErr(TAG, "vibrate");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_QLUTIL_PACKAGE, "vibrate", "()V")) {
		t.env->CallStaticVoidMethod(t.classID, t.methodID);
		t.env->DeleteLocalRef(t.classID);
	}
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	vibrateIOS();
#endif
}

// 折算为 0~100
int Util::getBattery() {
	WH_LogErr(TAG, "getBattery");
	int battery = 100;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_QLUTIL_PACKAGE, "getBattery", "()I")) {
		battery = t.env->CallStaticIntMethod(t.classID, t.methodID);
		t.env->DeleteLocalRef(t.classID);
	}

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	battery = getBatteryLevelIOS();
#endif

	WH_LogErr(TAG, "getBattery, battery=%d", battery);
	return battery;
}

// 折算为 0~100
int Util::getSignal() {
	WH_LogErr(TAG, "getSignal");
	int signal = 100;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_QLUTIL_PACKAGE, "getSignal", "()I")) {
		signal = t.env->CallStaticIntMethod(t.classID, t.methodID);
		t.env->DeleteLocalRef(t.classID);
	}

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	signal = getSignalIOS();
#endif

	WH_LogErr(TAG, "getSignal, signal=%d", signal);
	return signal;
}

void Util::keepScreenOn(bool flag) {
	WH_LogErr(TAG, "keepScreenOn");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_QLUTIL_PACKAGE, "keepScreenOn", "(Z)V")) {
		t.env->CallStaticVoidMethod(t.classID, t.methodID);
		t.env->DeleteLocalRef(t.classID);
	}
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#endif
}

// 得到设备id号
const char* Util::getUniqueCode() {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

	static std::string str = "";
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_QLUTIL_PACKAGE, "getIMEI", "()Ljava/lang/String;")) {
		jstring strJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
		str = JniHelper::jstring2string(strJava);
		t.env->DeleteLocalRef(strJava);
		t.env->DeleteLocalRef(t.classID);
	}
	WH_LogErr(TAG, "getUniqueCode, str=%s", str.c_str());
	return str.c_str();

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	WH_LogErr(TAG, "getUniqueCode, uuid=%s", getUniqueCodeIOS());
	return getUniqueCodeIOS();
#endif
	return "";
}

//升级
void Util::upgrade(string filename) {
	WH_LogErr(TAG, "upgrade");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	//const char* str;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_QLUTIL_PACKAGE, "updateVersion", "(Ljava/lang/String;)V")) {
		WH_LogErr(TAG, "初始化参数");
		jstring param = t.env->NewStringUTF(filename.c_str());
		t.env->CallStaticVoidMethod(t.classID, t.methodID,param);
		t.env->DeleteLocalRef(param);
		t.env->DeleteLocalRef(t.classID);
	}

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	WH_LogErr(TAG, "方法为实现");
#endif
}

bool Util::openApp(string filename) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	jboolean str;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPLIST_PACKAGE, "openApp", "(Ljava/lang/String;)Z")) {
		WH_LogErr(TAG, "初始化参数");
		jstring fn = t.env->NewStringUTF(filename.c_str());
		str = (jboolean)t.env->CallStaticBooleanMethod(t.classID, t.methodID,fn);
		t.env->DeleteLocalRef(fn);
		t.env->DeleteLocalRef(t.classID);
	}

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	WH_LogErr(TAG, "方法为实现");
	//vibrateIOS();
	//return (bool)str;
    return true;
#endif
	return false;
}

// 获得所有应用列表
char* Util::getAppList() {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	static std::string str = "";
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPLIST_PACKAGE, "read", "()Ljava/lang/String;")) {
		jstring strJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
		str = JniHelper::jstring2string(strJava);
		t.env->DeleteLocalRef(strJava);
		t.env->DeleteLocalRef(t.classID);
	}
	WH_LogErr(TAG, "applist json, str=%s", str.c_str());
	return const_cast<char*>(str.c_str());
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	WH_LogErr(TAG, "applist json, uuid=%s", getUniqueCodeIOS());
	return NULL;
#endif
	return NULL;
}

const char* Util::numToShortString(long long num) {
	if (num < 0)
	{
		return "0000";
	}

	__String *str = String::createWithFormat("%lld", num);
	const char* chars = str->getCString();

	int len = str->length();
	if (len > 8)
	{
		char numChars[4] = {0};
		for (int i=0; i<4; i++) {
			numChars[i] = chars[i];
		}
		int pointPos = len % 4;
		int afterPoint = 0;
		if (pointPos > 0)
		{
			for (int i=3; i>=pointPos; i--) {
				if (numChars[i] == '0')
				{
					numChars[i] = NULL;
				}
				else
				{
					afterPoint = i - pointPos + 1;
					break;
				}
			}
		}
		else
		{
			pointPos = 4;
		}

		char ret[6] = {0};
		int index = 0;
		for (; index<pointPos; index++) {
			ret[index] = numChars[index];
		}
		if (afterPoint > 0)
		{
			ret[index++] = '.';
			for (int i=0; i<afterPoint; i++)
			{
				ret[index] = numChars[index-1];
				index++;
			}
		}

		if (len > 12)
		{
			str->initWithFormat("%s%s", ret,U2F("万亿").c_str());
		}
		else
		{
			str->initWithFormat("%s%s", ret,U2F("亿").c_str());
		}

		chars = str->getCString();
	}
	else if (len > 4)
	{
		char numChars[4] = {0};
		for (int i=0; i<4; i++) {
			numChars[i] = chars[i];
		}
		int pointPos = len % 4;
		int afterPoint = 0;
		if (pointPos > 0)
		{
			for (int i=3; i>=pointPos; i--) {
				if (numChars[i] == '0')
				{
					numChars[i] = NULL;
				}
				else
				{
					afterPoint = i - pointPos + 1;
					break;
				}
			}
		}
		else
		{
			pointPos = 4;
		}

		char ret[6] = {0};
		int index = 0;
		for (; index<pointPos; index++) {
			ret[index] = numChars[index];
		}
		if (afterPoint > 0)
		{
			ret[index++] = '.';
			for (int i=0; i<afterPoint; i++)
			{
				ret[index] = numChars[index-1];
				index++;
			}
		}

		if (len > 8)
		{
			str->initWithFormat("%s%s", ret,U2F("亿").c_str());
		}
		else
		{
			str->initWithFormat("%s%s", ret,U2F("万").c_str());
		}

		chars = str->getCString();
	}

	WH_LogErr(TAG, "numToShortString, %lld, %s", num, chars);
	return chars;
}

void Util::saveCustomFaceImage(const unsigned char* path, WORD size,DWORD dwUserID)
{
	string szWritePath = FileUtils::getInstance()->getWritablePath();
	if(!szWritePath.empty())
	{
		CCLOG("接受头像消息 id:%d",dwUserID);
		FileUtils::getInstance()->addSearchPath(szWritePath);
		string wPath = szWritePath;
		if(szWritePath.find_last_of("/") != szWritePath.length()-1)
		{
			wPath = szWritePath.append("/");
		}
		wPath = wPath.append(StringUtils::toString(dwUserID)).append(".png");
		remove(wPath.c_str());
		FILE* file = fopen(wPath.c_str(), "wb+");
		if (file) {
			CCLOG("count the file content len = %d", size);
			size_t rLen = fwrite(path, sizeof(WORD), size, file);
			fclose(file);
		}
	}
}

char* Util::getPhoneNumber()
{
	WH_LogErr(TAG, "getPhoneNumber");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	static std::string str = "";
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_QLUTIL_PACKAGE, "getPhoneNumber", "()Ljava/lang/String;")) {
		jstring strJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
		str = JniHelper::jstring2string(strJava);
		t.env->DeleteLocalRef(strJava);
		t.env->DeleteLocalRef(t.classID);
		WH_LogErr(TAG, str.c_str());
	}
	return const_cast<char*>(str.c_str());

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	return "";
#endif
	return "";
}

//微信分享
void Util::weixinShare(string title, string text, int type, string url)
{
	if(SocketTCP::isFirstCheckInit()){
		return ;
	}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPACTIVITY_PACKAGE, "weixinShare", "(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)V")) {
		WH_LogErr(TAG, "初始化参数");
		jstring jtitle	= t.env->NewStringUTF(title.c_str());
		jstring jtext	= t.env->NewStringUTF(text.c_str());
		jstring jurl	= t.env->NewStringUTF(url.c_str());
		t.env->CallStaticVoidMethod(t.classID, t.methodID, jtitle, jtext, type, jurl);
		t.env->DeleteLocalRef(jtitle);
		t.env->DeleteLocalRef(jtext);
		t.env->DeleteLocalRef(jurl);
		t.env->DeleteLocalRef(t.classID);
	}
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	inviteIOS(title.c_str(),text.c_str(),type,url.c_str());
#endif
}

//微信炫耀
void Util::weChatResultShare(string roomId,string gold,string path)
{
	if(SocketTCP::isFirstCheckInit()){
		return ;
	}
	WH_LogErr("Util","start result share");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPACTIVITY_PACKAGE, "wechatResultShare", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) {
		WH_LogErr(TAG, "初始化参数");
		jstring room = t.env->NewStringUTF(roomId.c_str());
		jstring gs = t.env->NewStringUTF(gold.c_str());
		jstring paths = t.env->NewStringUTF(path.c_str());
		t.env->CallStaticVoidMethod(t.classID, t.methodID,room,gs,paths);
		t.env->DeleteLocalRef(room);
		t.env->DeleteLocalRef(gs);
		t.env->DeleteLocalRef(paths);
		t.env->DeleteLocalRef(t.classID);
	}
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    resultShareIOS(roomId.c_str(),gold.c_str(),path.c_str());
#endif
}

//微信分享
void Util::weChatInvite(string roomId,string username,string url)
{
	if(SocketTCP::isFirstCheckInit()){
		return ;
	}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPACTIVITY_PACKAGE, "weChatInvite", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) {
		WH_LogErr(TAG, "初始化参数");
		jstring jPath = t.env->NewStringUTF(roomId.c_str());
		jstring name = t.env->NewStringUTF(username.c_str());
		jstring jurl = t.env->NewStringUTF(url.c_str());
		t.env->CallStaticVoidMethod(t.classID, t.methodID,jPath,name,jurl);
		t.env->DeleteLocalRef(jPath);
		t.env->DeleteLocalRef(name);
		t.env->DeleteLocalRef(jurl);
		t.env->DeleteLocalRef(t.classID);
	}
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	inviteIOS(roomId.c_str(),username.c_str(), 0, url.c_str());
#endif
}

//获取可写路径[用来保存文件，更新资源]
std::string Util::getWritablePath()
{
	static std::string strExternalStorageDirectory	= "";
	if(strExternalStorageDirectory.length() > 0){
		return strExternalStorageDirectory;
	}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	//////////////////////////////////////////////////////////////////////////
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_QLUTIL_PACKAGE, "getExternalStorageDir", "()Ljava/lang/String;")) {
		jstring strJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
		t.env->DeleteLocalRef(t.classID);
		strExternalStorageDirectory = JniHelper::jstring2string(strJava);	//获取返回字符串
		WH_LogErr(TAG, strExternalStorageDirectory.c_str());
		t.env->DeleteLocalRef(strJava);
	}
	//////////////////////////////////////////////////////////////////////////
	std::string tmp					= getPackageNameJNI();
	if (tmp.length() > 0){
		strExternalStorageDirectory += "/";
		strExternalStorageDirectory += tmp;
		strExternalStorageDirectory += "/";
	}else{
		strExternalStorageDirectory += "/";
	}
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	strExternalStorageDirectory		= FileUtils::getInstance()->getWritablePath();
	std::size_t pos = 0;
	while ((pos = strExternalStorageDirectory.find('\\', pos)) != std::string::npos){
		strExternalStorageDirectory.replace(pos, 1, 1, '/');
	}
	//////////////////////////////////////////////////////////////////////////
	//结尾验证/
	if(strExternalStorageDirectory.length() > 0 && '/' != strExternalStorageDirectory.at(strExternalStorageDirectory.length() - 1)){
		strExternalStorageDirectory	+= "/";
	}
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	//纠正bug，路径导致资源读取不到的bug
	char exepath[MAX_PATH];
	std::string strdir = "", tmpdir	= "";
	memset(exepath, 0, MAX_PATH); 
	GetModuleFileNameA(NULL, exepath, MAX_PATH);
	strdir	= exepath;
	for (int i = strlen(exepath) - 1; i >= 0; --i){
		if(exepath[i] == '/' || exepath[i] == '\\'){
			tmpdir	= strdir.substr(0, i);
			tmpdir	+= "/";
			break;
		}
	}
	//////////////////////////////////////////////////////////////////////////
	std::size_t pos = 0;
	while ((pos = tmpdir.find('\\', pos)) != std::string::npos){
		tmpdir.replace(pos, 1, 1, '/');
	}
	//////////////////////////////////////////////////////////////////////////
	if(tmpdir.length() <= 0){
		strExternalStorageDirectory	= FileUtils::getInstance()->getWritablePath();
	}else{
		strExternalStorageDirectory	= tmpdir;
	}
#endif
	return strExternalStorageDirectory;
}

//获取可写路径[用来保存文件，更新资源]
bool Util::checkUpdate(std::string strUpdateUrl, std::string strWebUrl)
{
	UpgradeLayer * pUpgrade = UpgradeLayer::getInstance();
	if(pUpgrade->getAssetManager()->checkUpdateEx(strUpdateUrl)){
		//直接打开浏览器
		Util::openBrowser(strWebUrl);
	}
	pUpgrade->release();
	return true;
}

//获取可写路径[用来保存文件，更新资源]
std::string Util::GetIPv6(std::string mHost, std::string mPort)
{
#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	const char* pszIPV6 = getIPv6(mHost.c_str(), mPort.c_str());
	if(pszIPV6){
		string mIPv6 = pszIPV6;
		return mIPv6;
	}
	return "";
#else
	return mHost + "&&ipv4";
#endif
}

void Util::openBrowser(std::string strUrl)
{
	if(SocketTCP::isFirstCheckInit()){
		return ;
	}
	CCLog("================Util::openBrowser[%s]", strUrl.c_str());
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPACTIVITY_PACKAGE, "openBrowser", "(Ljava/lang/String;)V")) {
		WH_LogErr(TAG, "初始化参数");
		jstring jPath = t.env->NewStringUTF(strUrl.c_str());
		t.env->CallStaticVoidMethod(t.classID, t.methodID,jPath);
		t.env->DeleteLocalRef(jPath);
		t.env->DeleteLocalRef(t.classID);
	}
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
	OpenUrlIOS::getInstance()->OpenUrlFunc(strUrl.c_str());
#else
	MessageBox(StringUtils::format("url[%s]", strUrl.c_str()).c_str(), "Windows暂不支持webview");
#endif
}

//获取IPV6带connect检测
bool Util::GetIPTypeEx(std::string serverIp, unsigned short ServerPort, std::string& newServerIp, int& nFamilyType)
{
	if(SocketTCP::isFirstCheckInit()){
		return false;
	}
	CCLog("================Util::GetIPTypeEx[%s] [%d]", serverIp.c_str(), ServerPort);
	newServerIp	= serverIp;
	nFamilyType	= AF_INET;

	struct addrinfo* res0;
	struct addrinfo hints;
	struct addrinfo* res;
	int n, s;

	memset(&hints, 0, sizeof(hints));

	hints.ai_family		= PF_UNSPEC;
	hints.ai_socktype	= SOCK_STREAM;
	hints.ai_protocol	= IPPROTO_TCP;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	hints.ai_flags		= AI_PASSIVE;
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	//hints.ai_flags		= AI_PASSIVE;
#else
	hints.ai_flags		= AI_DEFAULT;
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	if((n=getaddrinfo(serverIp.c_str(), StringUtils::format("%d", ServerPort).c_str(), &hints, &res0))!=0){
		CCLog("================Util::GetIPTypeEx getaddrinfo error: [%s] [%d]", gai_strerror(n), hints.ai_flags);
		return false;
	}
#else
	if((n=getaddrinfo(serverIp.c_str(), "http", &hints, &res0))!=0){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
		CCLog("================Util::GetIPTypeEx getaddrinfo error: [%d] [%d]", WSAGetLastError(), hints.ai_flags);
#else
		CCLog("================Util::GetIPTypeEx getaddrinfo error: [%s] [%d]", gai_strerror(n), hints.ai_flags);
#endif
		return false;
	}
#endif

	struct sockaddr_in addr_in;
	struct sockaddr_in6 addr_in6;
	memset((void*)&addr_in, 0L, sizeof(addr_in));
	memset((void*)&addr_in6, 0L, sizeof(addr_in6));
	int connect_ret	= -1;;
	bool bConnect	= false;
	char ipbuf[65];
	int tempSocket;
	for (res = res0; res; res = res->ai_next) {
		connect_ret = -1;
		tempSocket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
		if (tempSocket < 0) {
			CCLog("================socket: ai_family[%d] ai_socktype[%d] ai_protocol[%d]", res->ai_family, res->ai_socktype, res->ai_protocol);
			continue;
		}
		memset(ipbuf, 0L, sizeof(ipbuf));
		if(AF_INET == res->ai_family){
			//IPV4
			addr_in = *((struct sockaddr_in*)res->ai_addr);
			memcpy(&addr_in, (struct sockaddr_in*)res->ai_addr, sizeof(addr_in));
			inet_ntop(AF_INET, &addr_in.sin_addr, ipbuf, sizeof(ipbuf));	// IPv6
			addr_in.sin_port = htons(ServerPort);
			connect_ret = connect(tempSocket, (struct sockaddr*)&addr_in, res->ai_addrlen);
		}else if(AF_INET6 == res->ai_family){
			//IPV6
			addr_in6  = *((struct sockaddr_in6*)res->ai_addr);
			memcpy(&addr_in6, (struct sockaddr_in6*)res->ai_addr, sizeof(addr_in6));
			inet_ntop(AF_INET6, &addr_in6.sin6_addr, ipbuf, sizeof(ipbuf));	// IPv6
			addr_in6.sin6_port = htons(ServerPort);
			connect_ret = connect(tempSocket, (struct sockaddr*)&addr_in6, res->ai_addrlen);
		}

		if (connect_ret < 0) {
			//没有链接成功
		}else{
			//链接成功
			bConnect	= true;
			nFamilyType	= res->ai_family;
			newServerIp	= ipbuf;
		}

		if (connect_ret < 0) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
			shutdown(tempSocket, 2);//0:不允许继续接受数据1:不允许继续发送数据2:不允许继续发送和接受数据
			closesocket(tempSocket);
#else
			shutdown(tempSocket, 2);//0:不允许继续接受数据1:不允许继续发送数据2:不允许继续发送和接受数据
			close(tempSocket);
#endif
			tempSocket = -1;
		}

		if(bConnect){
			break;
		}
	}

	freeaddrinfo(res0);

	CCLog("================socket: newServerIp[%s] nFamilyType[%d]", newServerIp.c_str(), nFamilyType);

	return bConnect;
}

//通过自己的控件打开网站
void Util::openWebView(std::string strWeb, int webNumber)
{
	if(SocketTCP::isFirstCheckInit()){
		return ;
	}
	//直接打开浏览器
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
	CWebView * pWebView = new CWebView();
	if(pWebView){
		pWebView->openURL(strWeb.c_str(), 1);
	}
#else
    OpenUrlIOS::getInstance()->OpenUrlFunc(strWeb.c_str());
#endif
}

void Util::lebianQueryUpdate()
{
	CCLog("================Util::lebianQueryUpdate");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPACTIVITY_PACKAGE, "lebianQueryUpdate", "()V")) {
		WH_LogErr(TAG, "乐变热更新");
		t.env->CallStaticVoidMethod(t.classID, t.methodID);
		t.env->DeleteLocalRef(t.classID);
	}
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
	WH_LogErr(TAG, "这个目前支持android");
#endif
}

//C++调用java平台代码
std::string Util::CallJavaPlatform(std::string strParam)
{
	std::string strRet	= "";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPACTIVITY_PACKAGE, "CallJavaPlatform", "(Ljava/lang/String;)Ljava/lang/String;")) {
		WH_LogErr(TAG, "初始化参数");
		jstring stringArg = t.env->NewStringUTF(strParam.c_str());
		jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, stringArg);
		t.env->DeleteLocalRef(t.classID);
		strRet = JniHelper::jstring2string(str);
		t.env->DeleteLocalRef(str);
		t.env->DeleteLocalRef(stringArg);
	}
#else

#endif
	return strRet;
}

void Util::PerFormFanWei(std::string strOrderID, std::string strAmount, std::string strUserID)
{
	if(SocketTCP::isFirstCheckInit()){
		return ;
	}
	CCLog("================Util::PerFormFanWei");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPACTIVITY_PACKAGE, "PerFormFanWeiPay", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) {
		WH_LogErr(TAG, "初始化参数");
		jstring stringOrderID	= t.env->NewStringUTF(strOrderID.c_str());
		jstring stringAmount	= t.env->NewStringUTF(strAmount.c_str());
		jstring stringUserID	= t.env->NewStringUTF(strUserID.c_str());
		t.env->CallStaticVoidMethod(t.classID, t.methodID, stringOrderID, stringAmount, stringUserID);
		t.env->DeleteLocalRef(t.classID);
		t.env->DeleteLocalRef(stringOrderID);
		t.env->DeleteLocalRef(stringAmount);
		t.env->DeleteLocalRef(stringUserID);
	}
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
	WH_LogErr(TAG, "这个目前支持android");
#endif
}

//获取剪切板内容
std::string Util::GetClipboardText() {
	if(SocketTCP::isFirstCheckInit()){
		return "";
	}
	CCLog("================Util::GetClipboardText");
	static std::string strText = "";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPACTIVITY_PACKAGE, "GetClipBoardText", "()Ljava/lang/String;")) {
		jstring strJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
		strText = JniHelper::jstring2string(strJava);
		t.env->DeleteLocalRef(strJava);
		t.env->DeleteLocalRef(t.classID);
	}
	WH_LogErr(TAG, "GetClipboardText, strText=%s", strText.c_str());
	return strText;
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	strText = GetClipboardTextIOS();
	WH_LogErr(TAG, "GetClipboardText, strText=%s", strText.c_str());
	return strText;
#endif
	return strText;
}

//复制到剪切板
void Util::CopyToClipboardText(std::string strClipboardText)
{
	if(SocketTCP::isFirstCheckInit()){
		return ;
	}
	CCLog("================Util::CopyToClipboardText");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPACTIVITY_PACKAGE, "CopyToClipboardText", "(Ljava/lang/String;)V")) {
		WH_LogErr(TAG, "初始化参数");
		jstring stringClipboardText	= t.env->NewStringUTF(strClipboardText.c_str());
		t.env->CallStaticVoidMethod(t.classID, t.methodID, stringClipboardText);
		t.env->DeleteLocalRef(t.classID);
		t.env->DeleteLocalRef(stringClipboardText);
	}
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
	CopyToClipboardTextIOS(strClipboardText.c_str());
#endif
}

int Util::SoundArmToWav(std::string pchAMRFileName,std::string pchWAVEFilename)
{
	if(SocketTCP::isFirstCheckInit()){
		return 2;
	}
	return ArmFun::Common_ArmToWav(pchAMRFileName, pchWAVEFilename);
}

//声音文件格式转换
int Util::SoundWavToArm(std::string pchWAVEFilename, std::string pchAMRFileName)
{
	if(SocketTCP::isFirstCheckInit()){
		return 2;
	}
	return ArmFun::Common_WavToArm(pchWAVEFilename, pchAMRFileName);
}

//打开内嵌浏览器
void Util::openMyWebView(std::string strUrl, float x, float y, float width, float height, int Horizontal)
{
	if(SocketTCP::isFirstCheckInit()){
		return ;
	}

	WH_LogErr("openMyWebView", "==========openMyWebView=x[%f]y[%f]w[%f]h[%f][%d]====", x, y, width, height, Horizontal);
	auto glview				= Director::getInstance()->getOpenGLView();
	Size designsize			= glview->getDesignResolutionSize();
	Size framesize			= glview->getFrameSize();
	float sx				= glview->getScaleX();
	float sy				= glview->getScaleY();

	Size designframe(framesize.width / sx, framesize.height / sy);
	WH_LogErr("openMyWebView", "==========openMyWebView=designframe w[%f]h[%f][%d]====", designframe.width, designframe.height, Horizontal);
	// 这里可能需要根据ResolutionPolicy进行修改。

	// Modify this ratio equation depend on your ResolutionPolicy.
	float ratiow			= designsize.width / framesize.width;
	float ratioh			= designsize.height / framesize.height;
	Vec2 orig((designframe.width - designsize.width) / 2, (designframe.height - designsize.height) / 2);
	WH_LogErr("openMyWebView", "==========openMyWebView=orig x[%f]y[%f]====", orig.x, orig.y);

	if(Horizontal == 1){
		//横版
		//x					= x / ratiow + orig.x / ratiow;
		//y					= y / ratioh + orig.y / ratioh;
		width				/= ratiow;
		height				/= ratioh;
		WH_LogErr("openMyWebView", "==========openMyWebView::openMyWebView=x[%f]y[%f]w[%f]h[%f]====", x, y, width, height);
	}else{
		//竖板
		//x					= x / ratiow + orig.x / ratiow;
		//y					= y / ratioh + orig.y / ratioh;
		width				/= ratiow;
		height				/= ratioh;
		WH_LogErr("openMyWebView", "==========openMyWebView::openMyWebView=x[%f]y[%f]w[%f]h[%f]====", x, y, width, height);
	}

	//直接打开浏览器
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo t;
	if (JniHelper::getStaticMethodInfo(t, ANDROID_APPACTIVITY_PACKAGE, "openMyWebView", "(Ljava/lang/String;IIIII)V")) {
		WH_LogErr(TAG, "初始化参数");
		jstring jstrUrl		= t.env->NewStringUTF(strUrl.c_str());
		jint jX				= (int)x;
		jint jY				= (int)y;
		jint jWidth			= (int)width;
		jint jHeight		= (int)height;
		jint jHorizontal	= (int)Horizontal;
		t.env->CallStaticVoidMethod(t.classID, t.methodID, jstrUrl, jX, jY, jWidth, jHeight, jHorizontal);
		t.env->DeleteLocalRef(t.classID);
		t.env->DeleteLocalRef(jstrUrl);
	}
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	openMyWebViewIOS(strUrl.c_str(), x, y, width, height, Horizontal);
#else
	MessageBox(StringUtils::format("url[%s]", strUrl.c_str()).c_str(), "Windows暂不支持webview");
#endif
}

//检测app更新
void Util::HSUpdateApp(const char* mAppID, const char* pStrCurrentVersion) {
	if(SocketTCP::isFirstCheckInit()){
		return ;
	}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	HSUpdateAppIOS(mAppID, pStrCurrentVersion);
#endif
}

//检测是否联通网络
bool Util::isUdpCheckNetWorking(const char* pszHostName/* = NULL*/) {
	hostent* host_entry = nullptr;
	if(pszHostName == nullptr){
		host_entry = gethostbyname("www.baidu.com");
	}else{
		host_entry = gethostbyname(pszHostName);
	}
	if(host_entry){
		return true;
	}
	return false;
}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#include "sdk/util/wechatutil/WeChatLoginAndroid.h"
extern "C"
{
	void Java_org_cocos2dx_cpp_AppActivity_onNotifyReconnectServer(JNIEnv * env ,jobject jthiz)
	{
		WH_LogErr(TAG, "onNotifyReconnectServer");
	}

	void Java_org_cocos2dx_cpp_AppActivity_onNotifyWeixinShareSuccess(JNIEnv * env ,jobject jthiz)
	{
		WH_LogErr(TAG, "on WeChat Invite Success");
		WeChatLogin::getInstance()->onShareSuccess();
		NotificationCenter::getInstance()->postNotification(WECHAT_INVITE_SUCCESS,nullptr);
	}

	void Java_org_cocos2dx_cpp_AppActivity_onWeChatLoginCode(JNIEnv *env,jobject thisz,jstring code,jstring appId,jstring appSecret)
	{
		WH_LogErr("Util",U2F("get code value").c_str());
		const char* pszCode		= env->GetStringUTFChars(code,NULL);
		const char* pszappid	= env->GetStringUTFChars(appId,NULL);
		const char* pszsecret	= env->GetStringUTFChars(appSecret,NULL);
		codeResponseAndroid(pszCode,pszappid,pszsecret);
		env->ReleaseStringUTFChars(code, pszCode);
		env->ReleaseStringUTFChars(appId, pszappid);
		env->ReleaseStringUTFChars(appSecret, pszsecret);
	}

	void Java_org_cocos2dx_cpp_AppActivity_onWeChatResultShare(JNIEnv *env,jobject thisz)
	{
		WH_LogErr("Util","result share notify");
		NotificationCenter::getInstance()->postNotification(WECHAT_RESULT_SHARE_SUCCESS,nullptr);
	}

	void Java_org_cocos2dx_cpp_AppActivity_onWMLoginRoom(JNIEnv *env,jobject thisz,jstring code)
	{
		const char* pszCode = env->GetStringUTFChars(code, NULL);
		WH_LogErr("Util",U2F("=====onWMLoginRoom获取code[%s]").c_str(), pszCode);

		tagMWRoomInfo ntagInfo;
		ntagInfo.uInsertRoomID	= std::atoi(pszCode);
		ntagInfo.fDelayTime		= 0.0f;
		NotificationCenter::getInstance()->postNotification(WM_LOGIN_ROOM_RESULT, (Object *) (&ntagInfo));
		env->ReleaseStringUTFChars(code, pszCode);
	}

	//通过java调用C++
	//兼容java调用C++的业务，直接传参字符串，自己编写字符串的格式action_param1_param2_......_paramn
	void Java_org_cocos2dx_cpp_AppActivity_onCallFunctionToDo(JNIEnv *env, jobject thisz, jstring code)
	{
		const char* pszCode = env->GetStringUTFChars(code, NULL);
		WH_LogErr("Util",U2F("=====onCallFunctionToDo[%s]").c_str(), pszCode);

		tagCallFunctionToDo ntagInfo;
		ZeroMemory(&ntagInfo, sizeof(ntagInfo));
		Util::createTCHARWithString(pszCode, ntagInfo.szParam);
		NotificationCenter::getInstance()->postNotification(CALL_FUNCTION_TODO, (Object *) (&ntagInfo));
		env->ReleaseStringUTFChars(code, pszCode);
	}
};
#endif

